package com.ihighest.app.infox.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.GroupAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * 群组界面
 * 
 * @author lkx Created at 2015-10-13 下午8:16:48
 * @version 1.0
 */
public class CircleGroupActivity extends BaseActivity {
	private ActivityTitle title;
	private ListView listview;
	private GroupAdapter adapter;

	private RelativeLayout rl_create_group_chat;
	private RelativeLayout rl_no_result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_circlegroup);
		initView();
	}

	/**
	 * 初始化绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-13 下午8:45:49
	 */
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.circleGroupActivityTitle);
		title.setTitle(getString(R.string.ciecleActivity_groupchat));
		title.setViewVersible(title.titleBack);
		title.setViewGone( title.titleSetting, title.titleRefresh, title.titleMsg, title.rightLayout,
				title.titleSearch);
		// title.setOtherImg(R.drawable.infox_title_add);
		rl_create_group_chat = (RelativeLayout) findViewById(R.id.rl_create_group_chat);
		rl_create_group_chat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent;
				if (TextUtils.isEmpty(SharedPreferencesKit.getString(CircleGroupActivity.this, Const.SP_KEY_USER_ID))) {
					intent = new Intent(CircleGroupActivity.this, LoginActivity.class);
					startActivity(intent);

				} else {

					intent = new Intent(CircleGroupActivity.this, LaunchGroupChatActivity.class);
					startActivity(intent);
				}
			}
		});
		listview = (ListView) findViewById(R.id.groupList);
		rl_no_result = (RelativeLayout) findViewById(R.id.rl_no_result);
		Api.get("chatgroup/list", ApiParam.create().addParam("size", Integer.MAX_VALUE).addParam("page", 1), new ApiCallback(this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				adapter = new GroupAdapter(CircleGroupActivity.this, result.getDatas());
				if (result.getDatas() == null || result.getDatas().length() <= 0) {
					rl_no_result.setVisibility(View.VISIBLE);
					listview.setVisibility(View.GONE);
				} else {
					rl_no_result.setVisibility(View.GONE);
					listview.setVisibility(View.VISIBLE);
					listview.setAdapter(adapter);
					adapter.notifyDataSetChanged();
				}
			}

			@Override
			public void handleFailure(Exception e, String msg) {
			}
		}, this);
	}
}
